import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

class Hua extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: '',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}
class HomePage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _HomePageState();
  
}
class _HomePageState extends State<HomePage> {
  static const loadingTag = '##loading##';  //表尾标记
  var _words = <String>[loadingTag];


  @override
  void initState() {
    super.initState();
    _retrieveData();
  }

  ListView listView1=ListView.builder(
      itemCount: 100,
      itemExtent: 50.0,
      itemBuilder: (BuildContext context,int index){
        return ListTile(title: Text('$index'),);
      });

  @override
  Widget build(BuildContext context) {
    ListView listView2=ListView.separated(
        itemBuilder: (context, index){
          //如果到了表尾
          if(_words[index] == loadingTag){
            //不足100条，继续获取数据
            if(_words.length - 1 < 100){
              //获取数据
              _retrieveData();
              //加载显示loading
              return Container(
                padding: const EdgeInsets.all(16.0),
                alignment: Alignment.center,
                child: SizedBox(
                  width: 24.0,
                  height: 24.0,
                  child: CircularProgressIndicator(strokeWidth: 2.0,), //进度指示器
                ),
              );
            }else{
              //已经加载了100条数据，不再获取数据。
              return Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(16.0),
                child: Text(
                  '没有更多了',
                  style: TextStyle(color: Colors.grey),
                ),
              );
            }
          }
          //显示单词列表项
          return ListTile(title: Text(_words[index]),);
        },
        separatorBuilder: (context,index)=> Divider(height: .0,),
        itemCount: _words.length
    );
    return Scaffold(
      appBar: AppBar(
        title: Text('滑动模块'),
      ),
      body: listView2
      );
  }
  void _retrieveData(){
    Future.delayed(Duration(seconds: 2)).then((value){ //Duration时间计数器
      setState((){
        //重新构建列表
        _words.insertAll(
          _words.length - 1,
          //每次生成20个单词
          generateWordPairs().take(20).map((e) => e.asPascalCase).toList(),
        );
      });
    });
  }
}
